package com.qianye.redisUtil.impl;

import com.qianye.exception.NullException.NullEnums;
import com.qianye.exception.NullException.NullException;
import com.qianye.redisUtil.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;


/**
 * @Author: hjz
 * @DataTime: 2024/9/18 22:54
 **/
@Component
public class RedisUtilsImpl implements RedisUtils {

    private static final Logger logger = LoggerFactory.getLogger(RedisUtilsImpl.class);

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 设置K-V
     * 有过期时间
     *
     * @param key
     * @param value
     * @param timeout
     * @param unit
     * @return Boolean
     */
    @Override
    public Boolean setForRedis(String key, Object value, Long timeout, TimeUnit unit) {
        try {
            if (timeout != null && unit != null) {
                redisTemplate.opsForValue().set(key, value, timeout, unit);
            } else {
                redisTemplate.opsForValue().set(key, value);
            }
            return true;
        } catch (Exception e) {
            logger.error("Failed to set key '{}' with value '{}' to Redis", key, value, e);
            return false;
        }
    }

    /**
     * 设置K-V
     * 永久
     *
     * @param key
     * @param value
     * @return Boolean
     */
    @Override
    public Boolean setForRedis(String key, Object value) {
        return setForRedis(key, value, null, null);
    }

    /**
     * 获取K-V
     * 单条value
     *
     * @param key
     * @return Object
     */
    @Override
    public Object getForRedis(String key) {
        try {
            return redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            logger.error("Failed to get value for key '{}' from Redis", key, e);
            return null;
        }
    }

    /**
     * 批量获取K-V
     *
     * @param keys
     * @return List<Object>
     */
    @Override
    public List<Object> multiGet(Collection<String> keys) throws NullException {
        Optional.ofNullable(keys);
        if (keys.isEmpty()){
            throw new NullException(NullEnums.NULL_List.getCode(), NullEnums.NULL_List.getDescription());
        }
        return redisTemplate.opsForValue().multiGet(keys);
    }

    /**
     * 删除K-V
     *
     * @param key
     * @return Boolean
     */
    @Override
    public Boolean deleteForRedis(String key) {
        try {
            redisTemplate.delete(key);
            return true;
        } catch (Exception e) {
            logger.error("Failed to delete key '{}' from Redis", key, e);
            return false;
        }
    }

    /**
     * 批量删除K-V
     *
     * @param keys
     * @return Long
     */
    @Override
    public Long multiDelete(Collection<String> keys) {
        try {
            return redisTemplate.delete(keys);
        } catch (Exception e) {
            logger.error("Failed to delete keys '{}' from Redis", keys, e);
            return 0L;
        }
    }

    /**
     * 设置过期时间
     *
     * @param key
     * @param timeout
     * @param unit
     * @return Boolean
     */
    @Override
    public Boolean expireForRedis(String key, Long timeout, TimeUnit unit) {
        try {
            redisTemplate.expire(key, timeout, unit);
            return true;
        } catch (Exception e) {
            logger.error("Failed to set expire time for key '{}' to Redis", key, e);
            return false;
        }
    }


}
